package ngames.com.sushitv.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Fearless on 2016/7/28.
 */
public class View_ViewGroup_Flowlayout extends ViewGroup{
    public View_ViewGroup_Flowlayout(Context context) {
        this(context,null);
    }

    public View_ViewGroup_Flowlayout(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public View_ViewGroup_Flowlayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int sizeWidth=MeasureSpec.getSize(widthMeasureSpec);
        int modeWidth=MeasureSpec.getMode(widthMeasureSpec);
        int sizeHeight=MeasureSpec.getSize(heightMeasureSpec);
        int modeHeight=MeasureSpec.getMode(heightMeasureSpec);

        //如果是wrap_content
        int width=0;
        int height=0;
        //记录每一行的宽度和高度
        int lineWidth=0;
        int lineHeight=0;
        //得到内部元素的个数
        int cCount=getChildCount();
        for(int i=0;i<cCount;i++){
            View child=getChildAt(i);
            //测量子View的宽和高
            measureChild(child,widthMeasureSpec,heightMeasureSpec);
            //得到LayoutParams
            MarginLayoutParams lp= (MarginLayoutParams) child.getLayoutParams();
            //子View占据的宽度和高度
            int childWidth=child.getMeasuredWidth()+lp.leftMargin+lp.rightMargin;
            int childHeight=child.getMeasuredHeight()+lp.topMargin+lp.bottomMargin;

            if(lineWidth+childWidth>sizeWidth-getPaddingLeft()-getPaddingRight()){
                //如果当前宽度大于容器宽度
                //对比得到最大的宽度
                width=Math.max(width,lineWidth);
                //重置行宽
                lineWidth=childWidth;
                //记录行高
                height+=lineHeight;

                lineHeight=childHeight;
            }else{//未换行
                //叠加行宽
                lineWidth+=childWidth;
                //得到当前行最大的高度
                lineHeight=Math.max(lineHeight,childHeight);
            }
            if(i==cCount-1){
                //如果到达最后一个控件
                width=Math.max(lineWidth,width);
                height+=lineHeight;
            }
        }
        //如果是wrap_content 就用计算出来的
        setMeasuredDimension(modeWidth==MeasureSpec.EXACTLY?sizeWidth:width+getPaddingLeft()+getPaddingRight()
                ,modeHeight==MeasureSpec.EXACTLY?sizeHeight:height+getPaddingTop()+getPaddingBottom());
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }
    //设置子view的位置

    //一行一行的存储所有的View
    private List<List<View>> mAllViews=new ArrayList<List<View>>();
    //每一行的高度
    private List<Integer> mLineHeight=new ArrayList<Integer>();
    @Override
    protected void onLayout(boolean b, int i, int i1, int i2, int i3) {
        mAllViews.clear();
        mLineHeight.clear();
        //拿到当前ViewGroup的宽度
        int width=getWidth();
        int height=getHeight();

        int lineWidth=0;
        int lineHeight=0;
        List<View> lineViews=new ArrayList<View>();
        int cCount=getChildCount();

        for(int ii=0;ii<cCount;ii++){
            View child=getChildAt(ii);
            MarginLayoutParams lp= (MarginLayoutParams) child.getLayoutParams();
            int childWidth=child.getMeasuredWidth();
            int childHeight=child.getMeasuredHeight();
            //如果比容器宽要换行
            if(childWidth+lineWidth+lp.leftMargin+lp.rightMargin>width-getPaddingLeft()-getPaddingRight()){
                //记录lineheight 和当前行的Views
                mLineHeight.add(lineHeight);
                mAllViews.add(lineViews);
                //重置行宽和行高
                lineWidth=0;
                lineHeight=childHeight+lp.topMargin+lp.bottomMargin;
                //重置View集合
                lineViews=new ArrayList<View>();
            }

            lineWidth+=childWidth+lp.leftMargin+lp.rightMargin;
            lineHeight=Math.max(lineHeight,childHeight+lp.topMargin+lp.bottomMargin);
            lineViews.add(child);
        }
        //最后一行
        mLineHeight.add(lineHeight);
        mAllViews.add(lineViews);
        //子View的位置
        int left=getPaddingLeft();
        int top=getPaddingTop();

        //行数
        int lineNum=mAllViews.size();

        for(int iii=0;iii<lineNum;iii++){
            //当前行的所有view
            lineViews=mAllViews.get(iii);
            lineHeight=mLineHeight.get(iii);

            for(int j=0;j<lineViews.size();j++){
                View child=lineViews.get(j);
                //如果隐藏就不显示
                if(child.getVisibility()==View.GONE){
                    continue;
                }
                MarginLayoutParams lp= (MarginLayoutParams) child.getLayoutParams();
                //获取当前控件的left right top button
                int lc=left+lp.leftMargin;
                int tc=top+lp.topMargin;
                int rc=lc+child.getMeasuredWidth();
                int bc=tc+child.getMeasuredHeight();
                //为子view布局
                child.layout(lc,tc,rc,bc);
                left+=child.getMeasuredWidth()+lp.leftMargin+lp.rightMargin;
            }
            left=getPaddingLeft();
            top+=lineHeight;
        }
    }
    /**
     *与当前ViewGroup对应的LayoutParams
     * */
    @Override
    public LayoutParams generateLayoutParams(AttributeSet attrs) {
        return new MarginLayoutParams(getContext(),attrs);
    }


}
